/*
 * Copyright (C) 2008 GeoScheduler team, as stated on <http://www.geoscheduler.org/>.
 * 
 * This file is part of GeoScheduler.
 * 
 * GeoScheduler is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * GeoScheduler is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public License
 * along with GeoScheduler. If not, see <http://www.gnu.org/licenses/>.
 */
package org.geoscheduler.android.tools;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import org.geoscheduler.model.Event.AlertType;

/**
 * Datetime tools
 * 
 * @author paja
 */
public class DateTimeHelper {

	public static final DateFormat dateFormat = SimpleDateFormat.getDateInstance(SimpleDateFormat.SHORT);
	
	public static final DateFormat timeFormat = SimpleDateFormat.getTimeInstance(SimpleDateFormat.SHORT);
	
	public static final DateFormat timezoneFormat = new SimpleDateFormat("z");
	public static final DateFormat timeparsingFormat = new SimpleDateFormat("h:mm a z");

	private static final DateFormat dateISO8601 = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
	
	public static final String formatISO8601(Date dateTime) {
		
		return dateISO8601.format(dateTime);
	}
	
	public static final Date parseISO8601(String dateTime) {
	
		if (dateTime == null) {
			return null;
		} else {
			try {
				return dateISO8601.parse(dateTime);
			} catch (ParseException e) {
				return null;
			}
		}
	}
	
	public static final boolean isAllDay(Date dateFrom, Date dateTo) {
		
		if (dateFrom == null || dateTo == null) {
			return true;
		}
		
		Calendar cal = Calendar.getInstance();
		cal.setTime(dateFrom);
		
		if (cal.get(Calendar.HOUR_OF_DAY) != 0 || cal.get(Calendar.MINUTE) != 0 || cal.get(Calendar.SECOND) != 0) {
			return false;
		}
		
		cal.setTime(dateTo);
		
		if (cal.get(Calendar.HOUR_OF_DAY) != 23 || cal.get(Calendar.MINUTE) != 59 || cal.get(Calendar.SECOND) != 59) {
			return false;
		}
		
		return true;
	}
	
	public static final Date getDateFrom(String dateFrom, String timeFrom, boolean allDay) {
		
		Date ret = null;
		
		try {
			ret = DateTimeHelper.dateFormat.parse(dateFrom);
		} catch (ParseException e) {
			/* TODO: what to do hear? */
		}
		
		/* If necessary, time information is added */
		if (!allDay) {
			try {
				// FIXME isn't there a better way of doing this?
				long timeFromAdd = DateTimeHelper.timeparsingFormat.parse(timeFrom + " " + DateTimeHelper.timezoneFormat.format(new Date())).getTime();
				ret.setTime(ret.getTime() + timeFromAdd);
			} catch (ParseException e) {
				/* TODO: what to do hear? */
			}
		}
		
		return ret;
	}

	public static final Date getDateTo(String dateTo, String timeTo, boolean allDay) {
		
		Date ret = null;
		
		try {
			ret = DateTimeHelper.dateFormat.parse(dateTo);
		} catch (ParseException e) {
			/* TODO: what to do hear? */
		}
		
		/* If necessary, time information is added */
		long timeToAdd = 0;
		if (allDay) {
			/* Number of seconds in one day */
			timeToAdd = (((23 * 60 + 59) * 60) + 59) * 1000; 
		} else {
			try {
				// FIXME isn't there a better way of doing this?
				timeToAdd = DateTimeHelper.timeparsingFormat.parse(timeTo + " " + DateTimeHelper.timezoneFormat.format(new Date())).getTime();
			} catch (ParseException e) {
				/* TODO: what to do hear? */
			}
		}
		
		/* Add times to dates */
		ret.setTime(ret.getTime() + timeToAdd);
		
		return ret;
	}

	public static final String getDateToString(AlertType alertType, Date dateFrom, Date dateTo) {
		
		String ret = "";
		
		if (alertType == AlertType.LOCATION_OR_TIME
				|| alertType == AlertType.LOCATION_TIME
				|| alertType == AlertType.LOCATION_XOR_TIME
				|| alertType == AlertType.TAG_AND_TIME
				|| alertType == AlertType.TAG_OR_TIME
				|| alertType == AlertType.TIME) {
		
			if (isAllDay(dateFrom, dateTo)) {
				ret = dateFormat.format(dateFrom) 
					+ " - " 
					+ dateFormat.format(dateTo);
			} else {
				ret = dateFormat.format(dateFrom) 
					+ " - " 
					+ dateFormat.format(dateTo) 
					+ " (TIME)";
			}
		}		
		
		return ret;
	}
}
